
package com.android.matmanual;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Telephony.Intents;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class FactoryTestAuto extends Activity {
	BroadcastReceiver mBroadcastReceiver;
	int mLockSound;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.factorytestautolayout);
		Settings.System.putInt(getContentResolver(),"IsMatTest",1);
		mLockSound = Settings.System.getInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1);
		Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,0);					

		 IntentFilter filter = new IntentFilter();
	        filter.addAction("com.android.RfAutoTestResult");
	        filter.addAction("com.android.PhoneLoopbackAutoTestResult");
	        filter.addAction("com.android.HeadsetLoopbackAutoTestResult");
	        mBroadcastReceiver = new BroadcastReceiver() {
	            public void onReceive(Context context, Intent intent) {
	                final String action = intent.getAction();
	                 if (action.equals("com.android.RfAutoTestResult")) {
	                	 int resultCode = intent.getIntExtra("result", 1);	
	                	 if(resultCode ==0)
	     				{
	     						     Intent intent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
			            intent1.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			            intent1.putExtra("state", 0);
			            intent1.putExtra("name", "h2w");
			            intent1.putExtra("microphone", 1);
			            sendBroadcast(intent1);
			            TestControl.SetCurResult(true);
			            TestControl.GetNextTest();

	                		sendBroadcast( new Intent(Intents.SECRET_CODE_ACTION,
	    		                    Uri.parse("android_secret_code://" + "PhoneLoopbackAutoTest")));	

	     				}
	     				else if(resultCode ==1)				
	     				{
	     						     Intent intent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
			            intent1.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			            intent1.putExtra("state", 0);
			            intent1.putExtra("name", "h2w");
			            intent1.putExtra("microphone", 1);
			            sendBroadcast(intent1);
			            TestControl.SetCurResult(false);
			            TestControl.GetNextTest();
	     					sendBroadcast( new Intent(Intents.SECRET_CODE_ACTION,
	    		                    Uri.parse("android_secret_code://" + "PhoneLoopbackAutoTest")));		

	     				}
	    			} 
	                else if(action.equals("com.android.PhoneLoopbackAutoTestResult"))
	                {
	               	 	int resultCode = intent.getIntExtra("result", 1);	
	               	 	if(resultCode ==0)
	    				{
	               	 		TestControl.SetCurResult(true);
				            String next_test = TestControl.GetNextTest();
		    				startActivityForResult(new Intent(next_test), TestControl.cur_test);	

	    				}
	    				else if(resultCode ==1)				
	    				{
	    					TestControl.SetCurResult(false);
				            String next_test = TestControl.GetNextTest();
				    				startActivityForResult(new Intent(next_test), TestControl.cur_test);

	    				}
	                }
	                else if(action.equals("com.android.HeadsetLoopbackAutoTestResult"))
	                {
	               	 	int resultCode = intent.getIntExtra("result", 1);	
	               	 	if(resultCode ==0)
	    				{
	               	 	TestControl.SetCurResult(true);
			            String next_test = TestControl.GetNextTest();
	    				startActivityForResult(new Intent(next_test), TestControl.cur_test);			

	    				}
	    				else if(resultCode ==1)				
	    				{
	    					TestControl.SetCurResult(false);
				            String next_test = TestControl.GetNextTest();
		    				startActivityForResult(new Intent(next_test), TestControl.cur_test);				
	    				}
	                }
	                //liuweibo end
	                
	            }
	        };
	        
	        registerReceiver(mBroadcastReceiver, filter);		
	}
	@Override
	protected void onDestroy() {
		Settings.System.putInt(getContentResolver(),"IsMatTest",0);
		Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,mLockSound);				
		unregisterReceiver(mBroadcastReceiver);
		super.onDestroy();
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if (MotionEvent.ACTION_DOWN == event.getAction()) {
			startActivityForResult(new Intent(TestControl.test_list[0]), 0);
			TestControl.Init(TestControl.AUTO_STATE);
			return true;
		}
		return false;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
	    	finish();
	    }
	    return true;
	}
	
	private boolean mVersionTestFlag = false;
	private boolean mSdcardTestFlag = false;
	private boolean mKeypadTestFlag = false;
	private boolean mVibratorTestFlag = false;
	private boolean mScreenDeadPointTestFlag = false;
	private boolean mLCDTestFlag = false;
	private boolean mSpeakerTestFlag = false;
	private boolean mChargingTestFlag = false;
	//private boolean mHeadsetTestFlag = false;
	private boolean mFMTestFlag = false;
	private boolean mTouchTestFlag = false;
	private boolean mGravityTestFlag = false;
	private boolean mFrontCameraTestFlag = false;
	private boolean mCameraTestFlag = false;
	private boolean mWifiTestFlag = false;
	private boolean mBluetoothTestFlag = false;
	
	private boolean mRFTestFlag = false;
	private boolean mPhoneLoopbackTestFlag = false;
	private boolean mRTCTestFlag = false;
	private boolean mSimTestFlag = false;
	private boolean mMultipleTPTestFlag = false;
	private boolean mHeadsetLoopbackTestFlag = false;
	
	private boolean mGpsTestFlag = false;
	private boolean mProximityTestFlag = false;
	private boolean mLightTestFlag = false;
	private boolean mMagneticFieldTestFlag = false;
	private boolean mFlashLightTestFlag = false;
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == TestControl.TRUE_RESULT) {
			TestControl.SetCurResult(true);
		} else if (resultCode == TestControl.FALSE_RESULT) {
			TestControl.SetCurResult(false);
		} else {
			finish();
			return;
		}		

		TestControl.cur_test = requestCode;
		String next_test = TestControl.GetNextTest();
		if (next_test != null) {
			if(next_test.equals("android.intent.action.RFTest"))
			{
				 sendBroadcast( new Intent(Intents.SECRET_CODE_ACTION,
		                    Uri.parse("android_secret_code://" + "RFAutoTest")));	
			}
			else if(next_test.equals("android.intent.action.HeadsetTest"))
			{
				sendBroadcast( new Intent(Intents.SECRET_CODE_ACTION,
	                    Uri.parse("android_secret_code://" + "HeadsetLoopbackAutoTest")));				
			}
			else
			{
				startActivityForResult(new Intent(next_test), TestControl.cur_test);
			}		
		} else {
			startActivity(new Intent("android.intent.action.TestResult"));
			finish();
		}
	}
}
		 